package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.FpShopSubjectDTO;
import com.financing.wap.service.IFpShopSubjectService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;

import net.sf.json.JSONArray;

@Controller("fpShopSubjectController")
@RequestMapping("/api/fpShopSubject")
public class FpShopSubjectController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpShopSubjectService fpShopSubjectService;
	
	@RequestMapping("/listFpShopSubject")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpShopSubjectDTO fpShopSubject,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpShopSubject list begin .......");
			
//			fpShopSubject.setBegin(pager.getStartIndex());
//			fpShopSubject.setPageSize(pager.getPageSize());
			
			int count = fpShopSubjectService.queryListCount(fpShopSubject);
			
			pager.setTotalCount(count);
			
			List<FpShopSubjectDTO> fpShopSubjectList = null;
			
			if(count > 0){
				fpShopSubjectList = fpShopSubjectService.queryList(fpShopSubject);
				for (int i = 0; i < fpShopSubjectList.size(); i++) {
					if(StringUtils.isNotBlank(fpShopSubjectList.get(i).getAnswer())){
						fpShopSubjectList.get(i).setAnswerList(JSONArray.fromObject(fpShopSubjectList.get(i).getAnswer()));
					}
					
					
				}
			}else{
				fpShopSubjectList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpShopSubjectList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpShopSubject list end .......");
		}catch(Exception e){
			e.printStackTrace();
			log.error("query fpShopSubject error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpShopSubject")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpShopSubjectDTO fpShopSubject){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpShopSubject  begin .......");
			
			DTOdateSetUtil.setCreateInfo(fpShopSubject);
			DTOdateSetUtil.setUpdateInfo(fpShopSubject);
			
			int id = fpShopSubjectService.insert(fpShopSubject);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpShopSubject list end .......");
		}catch(Exception e){
			log.error("add fpShopSubject error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpShopSubject")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpShopSubjectDTO fpShopSubject){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpShopSubject  begin .......");
			
			Integer id = fpShopSubject.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpShopSubjectDTO dto = fpShopSubjectService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpShopSubject  end .......");
		}catch(Exception e){
			log.error("query detail fpShopSubject error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpShopSubject")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpShopSubjectDTO fpShopSubject){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpShopSubject  begin .......");
			
			Integer id = fpShopSubject.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			fpShopSubjectService.delete(id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpShopSubject  end .......");
		}catch(Exception e){
			log.error("delete fpShopSubject error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpShopSubject")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpShopSubjectDTO fpShopSubject){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpShopSubject  begin .......");
			
			Integer id = fpShopSubject.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(fpShopSubject);
			
			fpShopSubjectService.update(fpShopSubject);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpShopSubject list end .......");
		}catch(Exception e){
			log.error("update fpShopSubject error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpShopSubject")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpShopSubjectDTO fpShopSubject){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpShopSubject  begin .......");
			
			Integer id = fpShopSubject.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(fpShopSubject);
			
			fpShopSubjectService.updateForce(fpShopSubject);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpShopSubject list end .......");
		}catch(Exception e){
			log.error("updateForce fpShopSubject error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
